package com.zmzncs.lmtc;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;

/**
 * SwaggerConfig
 */
@Configuration
@EnableOpenApi
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@ApiIgnore
public class Swagger2Config {

    /*引入Knife4j提供的扩展类*/
    private final OpenApiExtensionResolver openApiExtensionResolver;

    public Swagger2Config(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean
    public Docket customer() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(productApiInfo())
                .groupName("客户管理")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zmzncs.lmtc.module.customer.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Page.class) //添加忽略类型
                .securityContexts(getSecurityContexts())
                .securitySchemes(getSecuritySchemes());
    }

    @Bean
    public Docket promotion() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(productApiInfo())
                .groupName("服务项目")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zmzncs.lmtc.module.promotion.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Page.class) //添加忽略类型
                .securityContexts(getSecurityContexts())
                .securitySchemes(getSecuritySchemes());
    }

    @Bean
    public Docket order() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(productApiInfo())
                .groupName("订单")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zmzncs.lmtc.module.order.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Page.class) //添加忽略类型
                .securityContexts(getSecurityContexts())
                .securitySchemes(getSecuritySchemes());
    }

    @Bean
    public Docket park() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(productApiInfo())
                .groupName("停车场")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zmzncs.lmtc.module.park.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Page.class) //添加忽略类型
                .securityContexts(getSecurityContexts())
                .securitySchemes(getSecuritySchemes());
    }

    @Bean
    public Docket shop() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(productApiInfo())
                .groupName("商户")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zmzncs.lmtc.module.shop.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Page.class) //添加忽略类型
                .securityContexts(getSecurityContexts())
                .securitySchemes(getSecuritySchemes());
    }

    @Bean
    public Docket transactionRecord() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(productApiInfo())
                .groupName("财务管理")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zmzncs.lmtc.module.finance.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Page.class) //添加忽略类型
                .securityContexts(getSecurityContexts())
                .securitySchemes(getSecuritySchemes());
    }

    @Bean
    public Docket system() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(productApiInfo())
                .groupName("系统管理")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zmzncs.lmtc.module.system.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Page.class) //添加忽略类型
                .securityContexts(getSecurityContexts())
                .securitySchemes(getSecuritySchemes());
    }

    @Bean
    public Docket test() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(productApiInfo())
                .groupName("测试")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zmzncs.lmtc.module.test.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Page.class) //添加忽略类型
                .securityContexts(getSecurityContexts())
                .securitySchemes(getSecuritySchemes());
    }

    private ApiInfo productApiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("立码停车-后台管理端")
                .description("文档描述。。。")
                .version("1.0")
                .termsOfServiceUrl("API TERMS URL")
                .license("license")
                .licenseUrl("license url")
                .build();

        return apiInfo;
    }


    /**
     *  认证请求头
     */
    private List<SecurityScheme> getSecuritySchemes(){
        List<SecurityScheme> securitySchemes = Arrays.asList(
                new ApiKey("token", "token", "header")
        );

        return securitySchemes;
    }

    /**
     *  授权范围
     */
    private AuthorizationScope[] getAuthorizationScopes(){
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;

        return authorizationScopes;
    }

    /**
     *  认证目录
     */
    private List<SecurityContext> getSecurityContexts(){
        List<SecurityContext> securityContexts = Arrays.asList(
                SecurityContext.builder()
                        .securityReferences(Lists.newArrayList(new SecurityReference("token", getAuthorizationScopes())))
                        .forPaths(PathSelectors.regex("/.*"))
                        .build()
        );

        return securityContexts;
    }

}